"use strict";

/**
 * 服务器列表控制器
 */
angular.module("app").controller("configEditCtrl", function($scope, $http, $route, $location) {

	$scope.serverId = $route.current.pathParams.serverId;
	$scope.configType = $route.current.pathParams.configType;
	//经过encodeURIComponent编码处理了，如需要明文请使用 decodeURIComponent解码
	$scope.absolutePath = decodeURIComponent($route.current.pathParams.absolutePath);
	
	$scope.server = {};
	
	$scope.config = {
		serverId		:	$scope.serverId,
		configType		:   $scope.configType,
		absolutePath	:	$scope.absolutePath,
		content			:	null
	}
	
	$scope.editor = null;
	$http.post("/config/content", $scope.config).success(function(d) {
		if(d.success){
			$scope.config.content = d.ret.config.content;
			$scope.config.username = d.ret.config.username;
			$scope.config.host = d.ret.config.host;
			$scope.config.filename = d.ret.config.filename;
			$scope.config.content = d.ret.config.content;
			$scope.config.fileLen = d.ret.config.fileLen;
			$scope.config.createTime = d.ret.config.createTime;
			$scope.config.lastUpdate = d.ret.config.lastUpdate;
			
			$("#editor").text($scope.config.content);
			
			/**
			 * 初始化ace-editor
			 */
			$scope.editor = ace.edit("editor");
			$scope.editor.setTheme("ace/theme/github");
			$scope.editor.session.setMode("ace/mode/sh");
			$scope.editor.setAutoScrollEditorIntoView(true);
			$scope.editor.setOption("maxLines", 999);
			
		}
	});
	
	$http.get("/server/get?serverId=" + $scope.serverId,{
		cache	:	true
	}).success(function(d) {
		if(d.success){
			$scope.server = d.ret.server;
		}
	});
	
	$scope.submitChange = function(){
		$scope.config.content = $scope.editor.getValue();
		
		$http.post("/config/edit", $scope.config).success(function(d) {
            $.alert({
                title: false,
                content: d.message,
                confirm: function() {
                    if(d.success) {
                        location.reload();
                    }
            }});
		});
				
		
	}
});
